#
#    Project: mxPluginExec
#             http://www.edna-site.org
#
#    Copyright (C) 2008-2012 European Synchrotron Radiation Facility
#                            Grenoble, France
#
#    Principal authors:      Marie-Francoise Incardona (incardon@esrf.fr)
#                            Olof Svensson (svensson@esrf.fr) 
#
#    Contributing author:    Karl Levik (karl.levik@diamond.ac.uk)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    and the GNU Lesser General Public License  along with this program.  
#    If not, see <http://www.gnu.org/licenses/>.
#

__authors__ = [ "Olof Svensson", "Marie-Francoise Incardona", "Karl Levik" ]
__contact__ = "svensson@esrf.fr"
__license__ = "LGPLv3+"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "20120712"
__status__ = "production"

import os

from EDAssert    import EDAssert
from EDUtilsTest import EDUtilsTest
from EDUtilsFile import EDUtilsFile

from EDTestCasePluginUnit import EDTestCasePluginUnit

from XSDataPhenixv1_1 import XSDataImage

class EDTestCasePluginUnitLabelitIndexingv1_1(EDTestCasePluginUnit):


    def __init__(self, _pyStrTestName=None):
        EDTestCasePluginUnit.__init__(self, "EDPluginLabelitIndexingv1_1")
        self.__strReferenceInputFile1 = os.path.join(self.getPluginTestsDataHome(), "XSDataImage_1_reference.xml")
        self.__strReferenceInputFile2 = os.path.join(self.getPluginTestsDataHome(), "XSDataImage_2_reference.xml")
        self.__strReferenceOutputFile = os.path.join(self.getPluginTestsDataHome(), "XSDataResultLabelitIndexing_reference.xml")




    def testParseLabelitScreenOutput(self):
        """
        This method test the parsing of the Labelit results in the log file.
        """
        edPluginLabelitIndexingv1_1 = self.createPlugin()
        strPathToLabelitLogText = os.path.join(self.getPluginTestsDataHome(), "labelit_v1000rc11.log")
        strLabelitLogText = self.readAndParseFile(strPathToLabelitLogText)
        xsDataLabelitScreenOutput = edPluginLabelitIndexingv1_1.parseLabelitScreenOutput(strLabelitLogText)
        strLabelitScreenOutputFile = os.path.join(self.getPluginTestsDataHome(), "XSDataLabelitScreenOutput_reference.xml")
        strLabelitScreenOutputXML = self.readAndParseFile(strLabelitScreenOutputFile)
        from XSDataPhenixv1_1 import XSDataLabelitScreenOutput
        xsDataLabelitScreenOutputReference = XSDataLabelitScreenOutput.parseString(strLabelitScreenOutputXML)
        EDAssert.equal(xsDataLabelitScreenOutputReference.marshal(), xsDataLabelitScreenOutput.marshal())


    def testParseMosflmScriptsOutput(self):
        """
        This method tests the parsing of the MOSFLM script generated by Labelit.
        """
        edPluginLabelitIndexingv1_1 = self.createPlugin()
        # First file from version 1000rc11
        strPathToMOSFLMScript = os.path.join(self.getPluginTestsDataHome(), "integration05_csh_v1000rc11.txt")
        strMOSFLMScript = self.readAndParseFile(strPathToMOSFLMScript)
        xsDataLabelitMosflmScriptsOutput = edPluginLabelitIndexingv1_1.parseMosflmScriptsOutput(strMOSFLMScript)
        xsDataLabelitMosflmScriptsOutput.outputFile("XSDataLabelitMosflmScriptsOutput_reference.xml")
        strLabelitMosflmScriptsOutputFile = os.path.join(self.getPluginTestsDataHome(), "XSDataLabelitMosflmScriptsOutput_reference.xml")
        strLabelitMosflmScriptsOutputXML = self.readAndParseFile(strLabelitMosflmScriptsOutputFile)
        from XSDataPhenixv1_1 import XSDataLabelitMosflmScriptsOutput
        xsDataLabelitMosflmScriptsOutputReference = XSDataLabelitMosflmScriptsOutput.parseString(strLabelitMosflmScriptsOutputXML)
        EDAssert.equal(xsDataLabelitMosflmScriptsOutputReference.marshal(), xsDataLabelitMosflmScriptsOutput.marshal())
        # Second file from version 1000b1
        strPathToMOSFLMScript = os.path.join(self.getPluginTestsDataHome(), "integration05_csh_v1000b1.txt")
        strMOSFLMScript = self.readAndParseFile(strPathToMOSFLMScript)
        xsDataLabelitMosflmScriptsOutput = edPluginLabelitIndexingv1_1.parseMosflmScriptsOutput(strMOSFLMScript)
        #sDataLabelitMosflmScriptsOutput.outputFile("XSDataLabelitMosflmScriptsOutput_v1000b1_reference.xml")
        strLabelitMosflmScriptsOutputFile = os.path.join(self.getPluginTestsDataHome(), "XSDataLabelitMosflmScriptsOutput_v1000b1_reference.xml")
        strLabelitMosflmScriptsOutputXML = self.readAndParseFile(strLabelitMosflmScriptsOutputFile)
        xsDataLabelitMosflmScriptsOutputReference = XSDataLabelitMosflmScriptsOutput.parseString(strLabelitMosflmScriptsOutputXML)
        EDAssert.equal(xsDataLabelitMosflmScriptsOutputReference.marshal(), xsDataLabelitMosflmScriptsOutput.marshal())


    def testGenerateMosflmScriptName(self):
        """
        This method tests the generateMosflmScriptName method of the Labelit plugin.
        """
        edPluginLabelitIndexingv1_1 = self.createPlugin()
        EDAssert.equal("integration01.csh", edPluginLabelitIndexingv1_1.generateMosflmScriptName(1))
        EDAssert.equal("integration10.csh", edPluginLabelitIndexingv1_1.generateMosflmScriptName(10))


    def testGenerateExecutiveSummary(self):
        """
        This method tests the generateExecutiveSummary of the Labelit plugin.
        It contains no assert call so the contents of the executive summary is not tested.
        """
        edPluginLabelitIndexingv1_1 = self.createPlugin()
        xmlInput1 = self.readAndParseFile(self.__strReferenceInputFile1)
        edPluginLabelitIndexingv1_1.setDataInput(xmlInput1, "referenceImage")
        xmlInput2 = self.readAndParseFile(self.__strReferenceInputFile1)
        edPluginLabelitIndexingv1_1.setDataInput(xmlInput2, "referenceImage")

        strLabelitScreenOutputFile = os.path.join(self.getPluginTestsDataHome(), "XSDataLabelitScreenOutput_reference.xml")
        strLabelitScreenOutputXML = self.readAndParseFile(strLabelitScreenOutputFile)
        from XSDataPhenixv1_1 import XSDataLabelitScreenOutput
        xsDataLabelitScreenOutputReference = XSDataLabelitScreenOutput.parseString(strLabelitScreenOutputXML)
        edPluginLabelitIndexingv1_1.setDataOutput(xsDataLabelitScreenOutputReference, "labelitScreenOutput")

        strLabelitMosflmScriptsOutputFile = os.path.join(self.getPluginTestsDataHome(), "XSDataLabelitMosflmScriptsOutput_reference.xml")
        strLabelitMosflmScriptsOutputXML = self.readAndParseFile(strLabelitMosflmScriptsOutputFile)
        from XSDataPhenixv1_1 import XSDataLabelitMosflmScriptsOutput
        xsDataLabelitMosflmScriptsOutputReference = XSDataLabelitMosflmScriptsOutput.parseString(strLabelitMosflmScriptsOutputXML)
        edPluginLabelitIndexingv1_1.setDataOutput(xsDataLabelitMosflmScriptsOutputReference, "mosflmScriptOutput")

        edPluginLabelitIndexingv1_1.generateExecutiveSummary(edPluginLabelitIndexingv1_1)


    def process(self):
        self.addTestMethod(self.testParseLabelitScreenOutput)
        self.addTestMethod(self.testParseMosflmScriptsOutput)
        self.addTestMethod(self.testGenerateMosflmScriptName)
        self.addTestMethod(self.testGenerateExecutiveSummary)


if __name__ == '__main__':

    EDTestCasePluginUnitLabelitIndexingv1_1 = EDTestCasePluginUnitLabelitIndexingv1_1("EDTestCasePluginUnitLabelitIndexingv1_1")
    EDTestCasePluginUnitLabelitIndexingv1_1.execute()

